<?php  
class ControllerProductArticle extends Controller {
	private $error = array(); 
	
	public function index() { 
		$this->language->load('product/article');
				
		$this->document->breadcrumbs = array();

		$this->document->breadcrumbs[] = array(
			'href'      => HTTP_SERVER . 'index.php?route=common/home',
			'text'      => $this->language->get('text_home'),
			'separator' => FALSE
		);
		
		$this->load->model('tool/seo_url'); 
		
		$this->load->model('catalog/cat');	
		
		if (isset($this->request->get['paths'])) {
			$paths = '';
				
			foreach (explode('_', $this->request->get['paths']) as $paths_id) {
				$cat_info = $this->model_catalog_cat->getCat($paths_id);
				
				if (!$paths) {
					$paths = $paths_id;
				} else {
					$paths .= '_' . $paths_id;
				}
				
				if ($cat_info) {
					$this->document->breadcrumbs[] = array(
						'href'      => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/cat&paths=' . $paths),
						'text'      => $cat_info['name'],
						'separator' => $this->language->get('text_separator')
					);
				}
			}
		}
		
		
		if (isset($this->request->get['keyword'])) {
			$url = '';

			if (isset($this->request->get['cat_id'])) {
				$url .= '&cat_id=' . $this->request->get['cat_id'];
			}	
			
			if (isset($this->request->get['description'])) {
				$url .= '&description=' . $this->request->get['description'];
			}
			
			$this->document->breadcrumbs[] = array(
				'href'      => HTTP_SERVER . 'index.php?route=product/search&keyword=' . $this->request->get['keyword'] . $url,
				'text'      => $this->language->get('text_search'),
				'separator' => $this->language->get('text_separator')
			);	
		}
		
		$this->load->model('catalog/article');
		
		if (isset($this->request->get['article_id'])) {
			$article_id = $this->request->get['article_id'];
		} else {
			$article_id = 0;
		}
		
		$article_info = $this->model_catalog_article->getArticle($article_id);
		
		if ($article_info) {
			$url = '';
			
			if (isset($this->request->get['paths'])) {
				$url .= '&paths=' . $this->request->get['paths'];
			}
			
			if (isset($this->request->get['manufacturer_id'])) {
				$url .= '&manufacturer_id=' . $this->request->get['manufacturer_id'];
			}			

			if (isset($this->request->get['keyword'])) {
				$url .= '&keyword=' . $this->request->get['keyword'];
			}			

			if (isset($this->request->get['cat_id'])) {
				$url .= '&cat_id=' . $this->request->get['cat_id'];
			}
				
			if (isset($this->request->get['description'])) {
				$url .= '&description=' . $this->request->get['description'];
			}				
									
			$this->document->breadcrumbs[] = array(
				'href'      => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/article' . $url . '&article_id=' . $this->request->get['article_id']),
				'text'      => $article_info['name'],
				'separator' => $this->language->get('text_separator')
			);			
			
			$this->document->title = $article_info['name'];
			
			$this->document->keywords = $article_info['meta_keywords'];
			
			$this->document->description = $article_info['meta_description'];

			$this->document->links = array();
	
			$this->document->links[] = array(
				'href' => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/article&article_id=' . $this->request->get['article_id']),
				'rel'  => 'canonical'
			);

			$this->data['heading_title'] = $article_info['name'];

			$this->data['text_enlarge'] = $this->language->get('text_enlarge');
			$this->data['text_discount'] = $this->language->get('text_discount');
			$this->data['text_options'] = $this->language->get('text_options');
			$this->data['text_price'] = $this->language->get('text_price');
			$this->data['text_availability'] = $this->language->get('text_availability');
			$this->data['text_model'] = $this->language->get('text_model');
			$this->data['text_manufacturer'] = $this->language->get('text_manufacturer');
			$this->data['text_order_quantity'] = $this->language->get('text_order_quantity');
			$this->data['text_price_per_item'] = $this->language->get('text_price_per_item');
			$this->data['text_qty'] = $this->language->get('text_qty');
			$this->data['text_write'] = $this->language->get('text_write');
			$this->data['text_average'] = $this->language->get('text_average');
			$this->data['text_no_rating'] = $this->language->get('text_no_rating');
			$this->data['text_note'] = $this->language->get('text_note');
			$this->data['text_no_images'] = $this->language->get('text_no_images');
			$this->data['text_no_related'] = $this->language->get('text_no_related');
			$this->data['text_wait'] = $this->language->get('text_wait');
			$this->data['text_tags'] = $this->language->get('text_tags');
			$this->data['text_minimum'] = sprintf($this->language->get('text_minimum'), $article_info['minimum']);

			$this->data['entry_name'] = $this->language->get('entry_name');
			$this->data['entry_review'] = $this->language->get('entry_review');
			$this->data['entry_rating'] = $this->language->get('entry_rating');
			$this->data['entry_good'] = $this->language->get('entry_good');
			$this->data['entry_bad'] = $this->language->get('entry_bad');
			$this->data['entry_captcha'] = $this->language->get('entry_captcha');

			$this->data['button_continue'] = $this->language->get('button_continue');
			
			$this->load->model('catalog/review');

			$this->data['tab_description'] = $this->language->get('tab_description');
			$this->data['tab_image'] = $this->language->get('tab_image');
			$this->data['tab_review'] = sprintf($this->language->get('tab_review'), $this->model_catalog_review->getTotalReviewsByProductId($this->request->get['article_id']));
			$this->data['tab_related'] = $this->language->get('tab_related');
			
			if ($this->config->get('config_review')) {
				$average = $this->model_catalog_review->getAverageRating($this->request->get['article_id']);	
			} else {
				$average = false;
			}
			
			$this->data['review_status'] = $this->config->get('config_review');
			
			$this->data['text_stars'] = sprintf($this->language->get('text_stars'), $average);
			
			$this->data['button_add_to_cart'] = $this->language->get('button_add_to_cart');

			$this->data['action'] = HTTP_SERVER . 'index.php?route=checkout/cart';
			
			$this->data['redirect'] = HTTP_SERVER . 'index.php?route=product/article' . $url . '&article_id=' . $this->request->get['article_id'];

			$this->load->model('tool/image');

			if ($article_info['image']) {
				$image = $article_info['image'];
			} else {
				$image = 'no_image.jpg';
			}	
					
			$this->data['popup'] = $this->model_tool_image->resize($image, $this->config->get('config_image_popup_width'), $this->config->get('config_image_popup_height'));
			$this->data['thumb'] = $this->model_tool_image->resize($image, $this->config->get('config_image_thumb_width'), $this->config->get('config_image_thumb_height'));
			$this->data['image'] = $this->model_tool_image->resize($image, 200, 200);
			$this->data['article_info'] = $article_info;
			
			if (isset($this->request->get['paths'])) {
				$paths = '';
					
				if(explode('_', $this->request->get['paths'])) {
					
					$paths_root=explode('_', $this->request->get['paths']);
					$paths=$paths_root[0];
				}else{
					$paths=$this->request->get['paths'];
				}
				$root=$this->getRootParent($paths);
				$this->data['root_name']="";
				$root_info=$this->model_catalog_cat->getCat($root);
				if($root_info){
					$this->data['root_name']=$root_info['name'];
					$this->data['root_href']= $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/cat&paths=' . $root_info['cat_id']);
				}
				
			}
			
			
			
			
			$this->data['meta_description'] = $article_info['meta_description'];
			$this->data['model'] = $article_info['model'];
			
			$this->data['description'] = html_entity_decode($article_info['description'], ENT_QUOTES, 'UTF-8');
			$this->data['article_id'] = $this->request->get['article_id'];
			
			$this->data['date_available'] = date('l d-m-Y', strtotime($article_info['date_available']));
			$download_total = $this->model_catalog_article->getTotalDownloads($this->request->get['article_id']);
			$this->data['downloads'] = array();
			if ($download_total) {
				$this->data['text_date_added'] = $this->language->get('text_date_added');
				$this->data['text_name'] = $this->language->get('text_name');
				$this->data['text_remaining'] = $this->language->get('text_remaining');
				$this->data['text_size'] = $this->language->get('text_size');
				$this->data['text_download'] = $this->language->get('text_download');
				
				$this->data['button_continue'] = $this->language->get('button_continue');		
				
				
				$results = $this->model_catalog_article->getDownloadsByArticleId($this->request->get['article_id'],0,5);
				foreach ($results as $result) {
					if (file_exists(DIR_DOWNLOAD . $result['filename'])) {
						$size = filesize(DIR_DOWNLOAD . $result['filename']);						
						$i = 0;
	
						$suffix = array(
							'B',
							'KB',
							'MB',
							'GB',
							'TB',
							'PB',
							'EB',
							'ZB',
							'YB'
						);
	
						while (($size / 1024) > 1) {
							$size = $size / 1024;
							$i++;
						}
	
						$this->data['downloads'][]= array(
							'download_id'   => $result['download_id'],
							'date_added' => date($this->language->get('date_format_short'), strtotime($result['date_added'])),
							'name'       => $result['name'],
							'remaining'  => $result['remaining'],
							'size'       => round(substr($size, 0, strpos($size, '.') + 4), 2) . $suffix[$i],
							'href'       => HTTPS_SERVER . 'index.php?route=product/article/download&download_id=' . $result['download_id']
						);
					}
				}
			}
			
			$this->data['articles'] = array();
			
			$results = $this->model_catalog_article->getArticleRelated($this->request->get['article_id']);
			
			foreach ($results as $result) {
				if ($result['image']) {
					$image = $result['image'];
				} else {
					$image = 'no_image.jpg';
				}
			
				
				$this->data['articles'][] = array(
					'article_id'    => $result['article_id'],
					'name'    		=> $result['name'],
					'model'   		=> $result['model'],
					'thumb'   		=> $this->model_tool_image->resize($image, $this->config->get('config_image_related_width'), $this->config->get('config_image_related_height')),
					'href'    		=> $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/article&article_id=' . $result['article_id']),
					'date_available' => date('d-m-Y', strtotime($result['date_available']))
					
				);
			}

			
			
			$this->model_catalog_article->updateViewed($this->request->get['article_id']);
			
			$this->data['tags'] = array();
					
			$results = $this->model_catalog_article->getArticleTags($this->request->get['article_id']);
			
			foreach ($results as $result) {
				if ($result['tag']) {
					$this->data['tags'][] = array(
						'tag'	=> $result['tag'],
						'href'	=> HTTP_SERVER . 'index.php?route=product/search&keyword=' . $result['tag']
					);
				}
			}
			
			if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/article.tpl')) {
				$this->template = $this->config->get('config_template') . '/template/product/article.tpl';
			} else {
				$this->template = 'default/template/product/article.tpl';
			}
			
			$this->children = array(
				'common/column_right',
				'common/column_left',
				'common/footer',
				'common/header'
			);
			
			$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
		} else {
			$url = '';
			
			if (isset($this->request->get['paths'])) {
				$url .= '&paths=' . $this->request->get['paths'];
			}
			
			if (isset($this->request->get['manufacturer_id'])) {
				$url .= '&manufacturer_id=' . $this->request->get['manufacturer_id'];
			}			

			if (isset($this->request->get['keyword'])) {
				$url .= '&keyword=' . $this->request->get['keyword'];
			}			

			if (isset($this->request->get['cat_id'])) {
				$url .= '&cat_id=' . $this->request->get['cat_id'];
			}
				
			if (isset($this->request->get['description'])) {
				$url .= '&description=' . $this->request->get['description'];
			}		
					
      		$this->document->breadcrumbs[] = array(
        		'href'      => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/article' . $url . '&article_id=' . $article_id),
        		'text'      => $this->language->get('text_error'),
        		'separator' => $this->language->get('text_separator')
      		);			
		
      		$this->document->title = $this->language->get('text_error');

      		$this->data['heading_title'] = $this->language->get('text_error');

      		$this->data['text_error'] = $this->language->get('text_error');

      		$this->data['button_continue'] = $this->language->get('button_continue');

      		$this->data['continue'] = HTTP_SERVER . 'index.php?route=common/home';
	  
			if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/error/not_found.tpl')) {
				$this->template = $this->config->get('config_template') . '/template/error/not_found.tpl';
			} else {
				$this->template = 'default/template/error/not_found.tpl';
			}
			
			$this->children = array(
				'common/column_right',
				'common/column_left',
				'common/footer',
				'common/header'
			);
			
			$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
    	}
  	}
	
	public function review() {
    	$this->language->load('product/article');
		
		$this->load->model('catalog/review');

		$this->data['text_no_reviews'] = $this->language->get('text_no_reviews');

		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}  
		
		$this->data['reviews'] = array();
			
		$results = $this->model_catalog_review->getReviewsByProductId($this->request->get['article_id'], ($page - 1) * 5, 5);
      		
		foreach ($results as $result) {
        	$this->data['reviews'][] = array(
        		'author'     => $result['author'],
				'rating'     => $result['rating'],
				'text'       => strip_tags($result['text']),
        		'stars'      => sprintf($this->language->get('text_stars'), $result['rating']),
        		'date_added' => date($this->language->get('date_format_short'), strtotime($result['date_added']))
        	);
      	}			
		
		$review_total = $this->model_catalog_review->getTotalReviewsByProductId($this->request->get['article_id']);
			
		$pagination = new Pagination();
		$pagination->total = $review_total;
		$pagination->page = $page;
		$pagination->limit = 5; 
		$pagination->text = $this->language->get('text_pagination');
		$pagination->url = HTTP_SERVER . 'index.php?route=product/article/review&article_id=' . $this->request->get['article_id'] . '&page={page}';
			
		$this->data['pagination'] = $pagination->render();

		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/review.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/product/review.tpl';
		} else {
			$this->template = 'default/template/product/review.tpl';
		}
		
		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
	}
	
	public function write() {
		$this->language->load('product/article');
		
		$this->load->model('catalog/review');
		
		$json = array();
		
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
			$this->model_catalog_review->addReview($this->request->get['article_id'], $this->request->post);
			
			$json['success'] = $this->language->get('text_success');
		} else {
			$json['error'] = $this->error['message'];
		}	
		
		$this->load->library('json');
		
		$this->response->setOutput(Json::encode($json));
	}
	
	public function captcha() {
		$this->load->library('captcha');
		
		$captcha = new Captcha();
		
		$this->session->data['captcha'] = $captcha->getCode();
		
		$captcha->showImage();
	}
	
	private function validate() {
		if ((strlen(utf8_decode($this->request->post['name'])) < 3) || (strlen(utf8_decode($this->request->post['name'])) > 25)) {
			$this->error['message'] = $this->language->get('error_name');
		}
		
		if ((strlen(utf8_decode($this->request->post['text'])) < 25) || (strlen(utf8_decode($this->request->post['text'])) > 1000)) {
			$this->error['message'] = $this->language->get('error_text');
		}

		if (!$this->request->post['rating']) {
			$this->error['message'] = $this->language->get('error_rating');
		}

		if (!isset($this->session->data['captcha']) || ($this->session->data['captcha'] != $this->request->post['captcha'])) {
			$this->error['message'] = $this->language->get('error_captcha');
		}

		if (!$this->error) {
			return TRUE;
		} else {
			return FALSE;
		}	
	}
	public function download() {
		$this->load->model('catalog/article');
		
		if (isset($this->request->get['download_id'])) {
			$download_id = $this->request->get['download_id'];
		} else {
			$download_id = 0;
		}
		
		$download_info = $this->model_catalog_article->getDownload($download_id);				
		if ($download_info) {
			$file = DIR_DOWNLOAD . $download_info['filename'];
			$mask = basename($download_info['mask']);
			$mime = 'application/octet-stream';
			$encoding = 'binary';

			if (!headers_sent()) {
				if (file_exists($file)) {
					header('Pragma: public');
					header('Expires: 0');
					header('Content-Description: File Transfer');
					header('Content-Type: ' . $mime);
					header('Content-Transfer-Encoding: ' . $encoding);
					header('Content-Disposition: attachment; filename=' . ($mask ? $mask : basename($file)));
					header('Content-Length: ' . filesize($file));
				
					$file = readfile($file, 'rb');
				
					print($file);
				} else {
					exit('Error: Could not find file ' . $file . '!');
				}
			} else {
				exit('Error: Headers already sent out!');
			}
		
		} else {
			$this->redirect(HTTPS_SERVER . 'index.php?route=article/product');
		}
	}
	protected function getRootParent($cat_id){
		$result = $this->model_catalog_cat->getCat($cat_id);
		if($result['parent_id']!=0){
			$root=$this->getRootParent($result['parent_id']);
		}else{
			$root=$result['cat_id'];
		}
		return $root;
	}
}
?>